//
//  GovWindwoDetailViewController.m
//  homeland
//
//  Created by SunShine on 14-1-19.
//  Copyright (c) 2014年 xzysun. All rights reserved.
//

#import "GovWindwoDetailViewController.h"

@interface GovWindwoDetailViewController ()

@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *titleList;
@end

@implementation GovWindwoDetailViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"详情";
    self.titleList = @[@"项目", @"部门", @"姓名", @"职务", @"电话"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table Method
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"GovWindowDetailCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0) {
        cell.textLabel.text = [self.dataItem objectForKey:@"project"];
    } else if (indexPath.section == 1) {
        cell.textLabel.text = [self.dataItem objectForKey:@"unitName"];
    } else if (indexPath.section == 2) {
        cell.textLabel.text = [self.dataItem objectForKey:@"name"];
    } else if (indexPath.section == 3) {
        cell.textLabel.text = [self.dataItem objectForKey:@"duty"];
    } else if (indexPath.section == 4) {
        cell.textLabel.text = [self.dataItem objectForKey:@"contactNumber"];
        cell.imageView.image = [UIImage imageNamed:@"xl_phone"];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, SCREEN_WIDTH-20, 24)];
    NSString *title = [self.titleList objectAtIndex:section];
    label.text = title;
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor blueColor];
    label.backgroundColor = [UIColor clearColor];
    UIView *panel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 34)];
    panel.backgroundColor = [UIColor clearColor];
    [panel addSubview:label];
    return panel;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 4) {
        NSString *msg = [NSString stringWithFormat:@"您是否需要拨打电话:%@", [self.dataItem objectForKey:@"contactNumber"]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.tag = PHONE_ALERT_TAG;
        [alert show];
    }
}

#pragma mark - Alert Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == PHONE_ALERT_TAG && buttonIndex == 1) {
        NSString *phone = [self.dataItem objectForKey:@"contactNumber"];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", phone]]];
    }
}
@end
